package view.conponent;

import com.alibaba.fastjson.JSON;
import model.hospital.Bed;
import model.hospital.Building;
import model.hospital.Floor;
import model.hospital.Room;
import net.ToServer;
import net.passClass.PassBuildings;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.util.TreeSet;

public class RoomManager extends JSplitPane {

    private final JTree tree = new JTree();

    public RoomManager() {
        tree.setModel(new DefaultTreeModel(getRoot()));


        JPanel treePanel = new JPanel();
        treePanel.setMaximumSize(new Dimension(200, 600));
        treePanel.setMinimumSize(new Dimension(200, 600));
        treePanel.setBackground(Color.WHITE);
        treePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        treePanel.add(tree);

        JScrollPane sp = new JScrollPane(treePanel);
        this.setLeftComponent(sp);
        this.setContinuousLayout(true);
        this.setDividerLocation(200);
        this.setDividerSize(8);

        Box vBox = Box.createVerticalBox();
        this.setRightComponent(vBox);

        Box textFieldBox = Box.createVerticalBox();
        textFieldBox.setMaximumSize(new Dimension(300, 300));
        textFieldBox.setMinimumSize(new Dimension(200, 300));
        vBox.add(textFieldBox);

        Box btnBox = Box.createVerticalBox();
        btnBox.setMaximumSize(new Dimension(300, 200));
        btnBox.setMinimumSize(new Dimension(200, 200));
        vBox.add(btnBox);

        Box box1 = Box.createHorizontalBox();
        JTextField buildingNumber = new JTextField(9);
        JLabel buildingNumberStr = new  JLabel("楼号");
        box1.add(buildingNumberStr);
        box1.add(buildingNumber);
        textFieldBox.add(box1);

        Box box2 = Box.createHorizontalBox();
        JTextField floorNumber = new JTextField(9);
        JLabel floorNumberStr = new  JLabel("楼层");
        box2.add(floorNumberStr);
        box2.add(floorNumber);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box2);

        Box box3 = Box.createHorizontalBox();
        JTextField roomNumber = new JTextField(9);
        JLabel roomNumberStr = new  JLabel("房号");
        box3.add(roomNumberStr);
        box3.add(roomNumber);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box3);

        Box box4 = Box.createHorizontalBox();
        JTextField bedNumber = new JTextField(9);
        JLabel bedNumberStr = new  JLabel("床号");
        box4.add(bedNumberStr);
        box4.add(bedNumber);
        textFieldBox.add(Box.createVerticalStrut(20));
        textFieldBox.add(box4);

        Box box5 = Box.createHorizontalBox();
        JTextField capacity = new JTextField(9);
        JLabel capacityStr = new  JLabel("设备容量");
        box5.add(capacityStr);
        box5.add(capacity);
        textFieldBox.add(Box.createVerticalStrut(20));
        textFieldBox.add(box5);

        Box box6 = Box.createHorizontalBox();
        JTextField type = new JTextField(9);
        JLabel typeStr = new  JLabel("设备类型");
        box6.add(typeStr);
        box6.add(type);
        textFieldBox.add(Box.createVerticalStrut(10));
        textFieldBox.add(box6);

        JButton addBtn = new JButton("添加");
        JButton deleteBtn = new JButton("删除");
        JButton refreshBtn = new JButton("刷新");

        btnBox.add(addBtn);
        btnBox.add(deleteBtn);
        btnBox.add(refreshBtn);

        addBtn.addActionListener(e -> {
            if(!bedNumber.getText().equals("")){
                ToServer.receive("/hospital/bed/add",
                        "buildingNumber:"+buildingNumber.getText()+"\n" +
                                "floorNumber:" +floorNumber.getText()+"\n" +
                                "roomNumber:" +roomNumber.getText()+"\n" +
                                "bedNumber:" +bedNumber.getText());
            } else {
                ToServer.receive("/hospital/room/add",
                        "buildingNumber:"+buildingNumber.getText()+"\n" +
                                "floorNumber:" +floorNumber.getText()+"\n" +
                                "roomNumber:" +roomNumber.getText()+"\n" +
                                "capacity:" +capacity.getText()+"\n" +
                                "type:" +type.getText());
            }
            requestData();
        });

        deleteBtn.addActionListener(e -> {
            ToServer.receive("/hospital/room/delete",
                    "buildingNumber:" + buildingNumber.getText() + "\n" +
                            "floorNumber:" + floorNumber.getText() + "\n" +
                            "roomNumber:" + roomNumber.getText() + "\n" +
                            "bedNumber:" + bedNumber.getText());
            requestData();
        });

        refreshBtn.addActionListener(e -> requestData()
        );

    }

    public void requestData(TreeSet<Building> buildings){
        tree.setModel(new DefaultTreeModel(getRoot(buildings)));
    }

    public void requestData(){

        PassBuildings data = JSON.parseObject(JSON.toJSONString(
                ToServer.receive("/hospital/get","\n")),PassBuildings.class);
        TreeSet<Building> buildings = data.getData();
        requestData(buildings);
    }

    public DefaultMutableTreeNode getRoot(){

        PassBuildings data = JSON.parseObject(JSON.toJSONString(
                ToServer.receive("/hospital/get","\n")),PassBuildings.class);
        TreeSet<Building> buildings = data.getData();
        return getRoot(buildings);
    }

    public DefaultMutableTreeNode getRoot(TreeSet<Building> buildings){
        return new DefaultMutableTreeNode("东软颐养社区中心"){
            {
                DefaultMutableTreeNode building;
                DefaultMutableTreeNode floor;
                DefaultMutableTreeNode room;
                DefaultMutableTreeNode bed;

                for(Building aBuilding : buildings){
                    building = new DefaultMutableTreeNode("建筑"+aBuilding.getBuildingNumber());
                    for(Floor aFloor : aBuilding.getFloors()){
                        floor = new DefaultMutableTreeNode("楼层"+aFloor.getFloorNumber());
                        for(Room aRoom : aFloor.getRooms()){
                            room = new DefaultMutableTreeNode("房间"+aRoom.getLocation().getRoomNumber());
                            if(aRoom.getEquipment()==null){
                                for(Bed aBed : aRoom.getBeds()){
                                    bed = new DefaultMutableTreeNode("床位"+aBed.getBedNumber());
                                    room.add(bed);
                                }
                            } else {
                                room.add(new DefaultMutableTreeNode("设备"+aRoom.getType()));
                            }
                            floor.add(room);
                        }
                        building.add(floor);
                    }
                    add(building);
                }

            }
        };
    }

}

